package kmu.lwl.imp.impl;

import kmu.lwl.entiy.Major;
import kmu.lwl.entiy.Student;
import kmu.lwl.factory.HibernateFactory;
import kmu.lwl.imp.SelectDao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

public class SelectDaoimpl implements SelectDao {
    private Student student;

    public Student selectMessage(String studentNO) {
        Session session = HibernateFactory.getSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("from Student where Id=?1").setParameter(1, studentNO).setMaxResults(1);
        student = (Student)query.uniqueResult();
        Query query1 = session.createQuery("from Major where majorId=?1").setParameter(1, student.getMajorId()).setMaxResults(1);
        Major major = (Major) query1.uniqueResult();
        student.setMajor(major);
        transaction.commit();
        session.close();
        return student;
    }

    @Override
    public List getAllMajor() {
        Session session = HibernateFactory.getSession();
        Transaction transaction = session.beginTransaction();
        List list = session.createQuery("from Major ").list();
        transaction.commit();
        session.close();
        return list;
    }
}
